package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Goods;
import com.manong.domain.Role;
import com.manong.mapper.GoodsMapper;
import com.manong.query.GoodsQueryObject;
import com.manong.service.GoodsService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/goods")
@CrossOrigin(allowCredentials = "true")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品列表
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "查询商品列表", expression = "goods:getList")
    public JsonResult list(GoodsQueryObject queryObject) {
        PageInfo pageInfo =  goodsService.query(queryObject);
        ResultData data = new ResultData(queryObject.getPageNum(),queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal()+""));
        return JsonResult.success(data);
    }

    /**
     * 删除商品接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "商品删除", expression = "goods:delete")
    public JsonResult delete(@PathVariable Long id) {
        goodsService.delete(id);
        return JsonResult.success();
    }


    /**
     * 新增或者修改商品
     * @param goods
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "商品新增或者编辑", expression = "goods:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Goods goods) {
        goodsService.saveOrUpdate(goods);
        return JsonResult.success();
    }

    /**
     * 查询单个商品
     * @param id
     * @return
     */
    @GetMapping("/getGoods/{id}")
    @RequiredPermission(name = "查询单个商品", expression = "goods:getGoods")
    public JsonResult getGoods(@PathVariable Long id) {
        Goods goods = goodsService.get(id);
        // JsonResult data属性 role
        return JsonResult.success(goods);
    }

    /**
     * 获取全部商品数量
     * @return
     */
    @GetMapping("/totalGoodsCount")
    @RequiredPermission(name = "查询全部商品数量", expression = "goods:totalGoodsCount")
    public JsonResult totalGoodsCount() {
        int totalGoodsCount = goodsService.getTotalGoodsCount();
        return JsonResult.success(totalGoodsCount);
    }
}
